package com.ruoyi.post.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.post.domain.Post;
import com.ruoyi.post.service.IPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 帖子Controller
 *
 * @author 劳雅玲
 * @date 2024-03-05
 */
@RestController
@RequestMapping("/post/post")//
public class PostController extends BaseController {
    @Autowired
    private IPostService postService;

    /**
     * 查询帖子列表
     */
    //权限控制
    @PreAuthorize("@ss.hasPermi('post:post:list')")
    //GetMapping意思是一个get请求，与上面的/post/post一起拼接
    @GetMapping("/list")
    public TableDataInfo list(Post post) {
        //设置请求分页数据
        startPage();
        post.setOkUserId(getUserId());
        //查询调用查询帖子列表
        List<Post> list = postService.selectPostList(post);
        return getDataTable(list);//返回给前端接口的数据
    }

    /**
     * 导出帖子列表
     */
    @PreAuthorize("@ss.hasPermi('post:post:export')")
    @Log(title = "帖子", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Post post) {
        List<Post> list = postService.selectPostList(post);
        ExcelUtil<Post> util = new ExcelUtil<Post>(Post.class);
        util.exportExcel(response, list, "帖子数据");
    }

    /**
     * 获取帖子详细信息
     */
    @PreAuthorize("@ss.hasPermi('post:post:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(postService.selectPostById(id));
    }

    /**
     * 新增帖子
     */
    @PreAuthorize("@ss.hasPermi('post:post:add')")
    @Log(title = "帖子", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Post post) {
        return toAjax(postService.insertPost(post));
    }

    /**
     * 修改帖子
     */
    @PreAuthorize("@ss.hasPermi('post:post:edit')")
    @Log(title = "帖子", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Post post) {
        return toAjax(postService.updatePost(post));
    }

    /**
     * 删除帖子
     */
    @PreAuthorize("@ss.hasPermi('post:post:remove')")
    //输出日志，可不用
    @Log(title = "帖子", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(postService.deletePostByIds(ids));
    }
}
